package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.TransactionAlerts;
import com.example.mall.dto.TransactionAlertsDto;
import com.example.mall.mapper.TransactionAlertsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.TransactionAlertsQuery;
import com.example.mall.service.ITransactionAlertsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.bouncycastle.crypto.engines.TnepresEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 异常交易报警表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class TransactionAlertsServiceImpl extends ServiceImpl<TransactionAlertsMapper, TransactionAlerts> implements ITransactionAlertsService {

    @Autowired
    private TransactionAlertsMapper transactionAlertsMapper;

    @Override
    public PageDTO<TransactionAlertsDto> queryPage(TransactionAlertsQuery transactionAlertsQuery) {
        if (transactionAlertsQuery == null) {
            transactionAlertsQuery = new TransactionAlertsQuery();
        }
        Page<TransactionAlerts> page = transactionAlertsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<TransactionAlerts> cartPage = lambdaQuery()
                .like(transactionAlertsQuery.getAlertId()!= null, TransactionAlerts::getAlertId, transactionAlertsQuery.getAlertId())
                .like(transactionAlertsQuery.getOrderId()!= null, TransactionAlerts::getOrderId, transactionAlertsQuery.getOrderId())
                .like(transactionAlertsQuery.getAlertType()!= null, TransactionAlerts::getAlertType, transactionAlertsQuery.getAlertType())
                .like(transactionAlertsQuery.getDescription()!= null, TransactionAlerts::getDescription, transactionAlertsQuery.getDescription())
                .like(transactionAlertsQuery.getSeverity()!= null, TransactionAlerts::getSeverity, transactionAlertsQuery.getSeverity())
                .like(transactionAlertsQuery.getHandledStatus()!= null, TransactionAlerts::getHandledStatus, transactionAlertsQuery.getHandledStatus())
                .like(transactionAlertsQuery.getHandledBy()!= null, TransactionAlerts::getHandledBy, transactionAlertsQuery.getHandledBy())
                .apply(transactionAlertsQuery.getAlertTime() != null,
                        "DATE_FORMAT(alert_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        transactionAlertsQuery.getAlertTime())
                .apply(transactionAlertsQuery.getHandledTime() != null,
                        "DATE_FORMAT(handled_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        transactionAlertsQuery.getHandledTime())
                .orderByDesc(TransactionAlerts::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, TransactionAlertsDto.class);
    }

    @Override
    public TransactionAlerts queryTransactionAlertsById(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        return transactionAlertsMapper.selectById(id);
    }

    @Override
    public List<TransactionAlerts> queryTransactionAlertsList() {
        return this.list();
    }

    @Override
    public void insert(TransactionAlertsDto transactionAlertsDto) {
        TransactionAlerts transactionAlerts = new TransactionAlerts();
        CopyUtils.copyProperties(transactionAlertsDto, transactionAlerts);
        transactionAlerts.setAlertId(CodeGenerateUtils.generateUniqueId());
        transactionAlerts.setHandledStatus(BaseConstants.NOT_USED);
        transactionAlerts.setCreateDate(new Date());
        transactionAlerts.setUpdateDate(new Date());
        transactionAlertsMapper.insert(transactionAlerts);
    }

    @Override
    public void update(TransactionAlertsDto transactionAlertsDto) {
        AssertUtil.isNotNull(transactionAlertsDto.getAlertId(), "id不能为空");
        TransactionAlerts transactionAlerts = transactionAlertsMapper.selectById(transactionAlertsDto.getAlertId());
        AssertUtil.isNotNull(transactionAlerts, "id不存在");
        CopyUtils.copyProperties(transactionAlertsDto, transactionAlerts);
        transactionAlerts.setUpdateDate(new Date());
        transactionAlertsMapper.updateById(transactionAlerts);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        TransactionAlerts transactionAlerts = transactionAlertsMapper.selectById(id);
        AssertUtil.isNotNull(transactionAlerts, "id不存在");
        transactionAlerts.setHandledStatus(BaseConstants.USED_ALREADY);
        transactionAlerts.setUpdateDate(new Date());
        transactionAlertsMapper.updateById(transactionAlerts);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids, "id不能为空");
        List<TransactionAlerts> transactionAlerts = transactionAlertsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(transactionAlerts, "id不存在");
        for (TransactionAlerts transactionAlert : transactionAlerts) {
            transactionAlert.setHandledStatus(BaseConstants.USED_ALREADY);
            transactionAlert.setUpdateDate(new Date());
            transactionAlertsMapper.updateById(transactionAlert);
        }
    }
}
